---
--类名: CaptainModel.lua
--创建日期: 2018-9-22
--创建人: 宋建邦
--功能描述: 船长室
--修改内容:

local CaptainModel = class("CaptainModel")

function CaptainModel:ctor(uid)
    self.uid = uid
end

function CaptainModel:getCaptainID()
    return self.uid
end

function CaptainModel:getOwnerInfo()
    return self.msg.AgentData.OwnerInfo
end

function CaptainModel:read(msg)
    self.msg = msg

    -- dump(msg, "captain data:")
    self.ServerData = self.msg.ServerData
    self.messageList = self.msg.ServerData.MessageData
    self.StringPars = self.msg.AgentData.StringPars
    self.MessageConfig = self.msg.ServerData.MessageConfig
    self.locationConfigs = self.msg.AgentData.location
end

-- 本地缓存的藏品数据，包含单独通知的藏品获取
function CaptainModel:localHasCollect(cid)
    if self:hasCollect(cid) then
        return true
    end

    if self.localCollect and self.localCollect[cid] then
        return true
    end

    return false
end

function CaptainModel:addLocalCollect(cid)
    self.localCollect = self.localCollect or {}

    self.localCollect[cid] = true
end

function CaptainModel:hasCollect(cid)
    if UD:hasSpCollector(cid) then
        return true
    end

    if self.msg == nil then
        return false
    end
    
    if self.msg.AgentData.Collectors then
        return table.indexof(self.msg.AgentData.Collectors, cid)
    end
end

function CaptainModel:collectUsed(cid)
    return self:collectLocation(cid) ~= 0
end

function CaptainModel:collectLocation(cid)
    local l = self:getLocationConfigs()
    if l then
        for i, v in ipairs(l) do
            if v.Furniture == cid then
                return v.ID
            end
        end
    end

    return 0
end

function CaptainModel:getMyCollectsByType(tid)
    tid = checknumber(tid)
    local ret = {}
    local ctl = app:getInst("CaptainController")
    if self.msg.AgentData.Collectors and ctl then
        for i, v in ipairs(self.msg.AgentData.Collectors) do
            local cfg = ctl.hCollectors[v]
            if cfg and checknumber(cfg.type) == tid then
                ret[#ret + 1] = cfg
            end
        end
    end
    return ret
end

function CaptainModel:collectConifg(cid)
    local ctl = app:getInst("CaptainController")
    if ctl then
        return ctl.hCollectors[cid]
    end
end

function CaptainModel:c3bFromCollectID(cid)
    local ccfg = self:collectConifg(cid)
    local cfgcolor = cc.c3b(255,255,255)
    if ccfg then
        cfgcolor = display.color3BWithString("#"..ccfg.colorStr)
    end
    return cfgcolor
end

function CaptainModel:locationConfig(lid)
    local ctl = app:getInst("CaptainController")
    if ctl then
        return ctl.hLocations[lid]
    end
end

function CaptainModel:locationId2Type(lid)    
    local ret = 0
    local cfg = self:locationConfig(lid)
    if cfg then
        ret = checknumber(cfg.type)
    end
    return ret
end

function CaptainModel:getCurrentCollectInLocation(lid)
    if self:getLocationConfigs() then
        for i, v in ipairs(self:getLocationConfigs()) do
            if v.ID == lid then
                return v.Furniture
            end
        end
    end
end

function CaptainModel:getArmatureID()
    return self.msg.AgentData.OwnerInfo.ClassID*10 + self.msg.AgentData.OwnerInfo.Gender
end

--clent data

function CaptainModel:getCaptainInfo()
    return app:sendMsg("CaptainController", "userBaseDataFromWho", self.msg.AgentData.OwnerInfo)
end

function CaptainModel:getOwnedCollectors()
    return self.msg.AgentData.Collectors
end

function CaptainModel:getLocationConfigs()
    return self.locationConfigs
end

function CaptainModel:resetLocationConfigs(cfg)
    self.locationConfigs = cfg
end

function CaptainModel:getStringConfig()
    return self.StringPars
end

function CaptainModel:resetStringConfig(cfg)
    self.StringPars = cfg
end

--server data
function CaptainModel:getServerData()
    return self.ServerData
end

function CaptainModel:resetServerData(sd)
    self.ServerData = sd

    self.messageList = self:getServerData().MessageData
    self.MessageConfig = self:getServerData().MessageConfig
end

function CaptainModel:getVisitNum()
    return self:getServerData().VisitedNum
end

function CaptainModel:getPraiseNum()
    return self:getServerData().PraisedNum
end

function CaptainModel:getRankData()
    return self:getServerData().InteractionData
end

function CaptainModel:resetMessages(msgdata)
    self.messageList = msgdata
end

function CaptainModel:getMessages()
    local messages = {}
    for i, v in pairs(self.messageList) do
        table.insert(messages, v)
    end

    table.sort(messages, function(a, b)
        return a.Stamp > b.Stamp
    end)

    return messages
end

function CaptainModel:getMessageConfig()
    return self.MessageConfig
end

function CaptainModel:resetMessageConfig(cfg)
    self.MessageConfig = cfg
end

function CaptainModel:getVisitors()
    local visitors = {}

    for i, v in pairs(self:getServerData().Visitors) do
        table.insert(visitors, v)
    end

    table.sort(visitors, function (a, b)
        return a.Stamp > b.Stamp
    end)

    return visitors
end

return CaptainModel